/**
 * 
 */
package tracksystem;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import managesystem.Student;
import managesystem.StudentManager;

/**
 * @author Incalza Dario
 *
 */
public class mobileOtherServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Calendar startDate = Calendar.getInstance();
	private Calendar stopDate = Calendar.getInstance();
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp){
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		
		/*
		 * Haal alle informatie op.
		 */
		String type = (String) req.getParameter("type");
		int duration = Integer.valueOf(req.getParameter("duration"));
		int rating = Integer.valueOf(req.getParameter("rating"));
		String description = (String) req.getParameter("description");
		String comment = (String) req.getParameter("comment");
		
		/**
		 * Genereer een stopDate omdat we nu met een duration werken.
		 */
		generateStopDate(duration);
		/**
		 * Aanmaken van een otherActivity en saven.
		 */
		OtherActivity oa = new OtherActivity(startDate.getTime(),stopDate.getTime(),null,description,rating,type,comment);
		currStud.addMobileActivity(oa);
		StudentManager.add(currStud);
		session.setAttribute("currStud",currStud);
		session.setAttribute("message","Other activity tracked succesfully.");
		
		try {
			resp.sendRedirect("/index-mobile.jsp");
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	/**
	 * Genereer een stopDate door de duration op te tellen bij het uur veld.
	 * @param duration
	 */
	private void generateStopDate(int duration){
		stopDate.add(Calendar.HOUR_OF_DAY,duration);
	}

}
